package gof23.structure.flyweight;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class JDKFlyweightDemo {

	public static void main(String[] args) throws Exception {
		String a = "123";
		String b = "123";
		System.out.println(a==b);
		
		StringBuilder builderA = new StringBuilder("1234");
		StringBuilder builderB = new StringBuilder("1234");
		//a=null;
		a = builderA.toString();
		b = builderB.toString();
		System.out.println(a==b);
		
		
		//线程池中享元了worker中的Thread
		ThreadPoolExecutor executors = new ThreadPoolExecutor(1,2,10L,TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(1));
		
		executors.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(10000L);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			System.out.println("NO.1"+Thread.currentThread().getName());
		
			}
		});
		
		executors.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000L);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			System.out.println("NO.2"+Thread.currentThread().getName());
		
			}
		});
	
		executors.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(100L);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			System.out.println("NO.3"+Thread.currentThread().getName());
		
			}
		});
		System.out.println("getActiveCount:"+executors.getActiveCount());
		
	}
	
}
